<?php

namespace App\Service\Index\v1_3_0;

use App\Constants\ErrorCode;
use App\Model\UserFreeCall;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 领取福利券
 */
class GetFreecallService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        //是否主播
        $userProfile = UserProfile::getProfile($userId);
        if ($userProfile->gender != 1) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '当前身份暂时无法领取');
        }
        //今天赠送免费券
        $freeCount = UserFreeCall::checkValid($userId, date('Y-m-d'), -2, 'count');
        if ($freeCount >= 1) {
            log_user_result($userId, "SendCount:{$freeCount}");
            return $this->setError(ErrorCode::FREE_CALL_GET, '已经领取过该免费通话券啦～');
        }
        $freeTime = 1;
        UserFreeCall::addRecord($userId, $freeTime, [], UserFreeCall::FROM_NEW);
        log_user_result($userId, "User==AddFreeCall:{$freeTime}");
        return ['result' => 1];
    }
}
